package com.dg.gaming.server;

import java.util.*;

import com.dg.gaming.api.common.*;
import com.dg.gaming.server.messages.*;
import javolution.util.*;
import nanoxml.*;
import org.xsocket.connection.*;
import java.util.zip.GZIPInputStream;
import java.io.ByteArrayInputStream;
import com.dg.gaming.api.common.util.StringZipper;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Worker implements Runnable {
//    private static Map<String,
//                       ServerConnectionThread> tblControllers = Collections.
//            synchronizedMap(new
//                            FastMap<String, ServerConnectionThread>());

    private static Map<String,
                       ServerTableHandler> tblControllers = Collections.
            synchronizedMap(new
                            FastMap<String, ServerTableHandler>());

//    public static void addConnection(TableController con) {
//        tblControllers.put(con.getId(), con);
//    }

    public static void addConnection(ServerTableHandler con) {
        tblControllers.put(con.getId(), con);
    }


    private AUserMessage userMessage = null;
    private WorkerQueue wq = null;


    public Worker(WorkerQueue wq, AUserMessage userMessage) {
        this.wq = wq;
        this.userMessage = userMessage;
    }

    public void run() {
        if (userMessage == null) {
            return;
        }
        if (userMessage instanceof DisconnectMessage) {

        } else if (userMessage instanceof UserMessage) {
            byte[] bt = userMessage.getMessage();

            XMLElement xml = new XMLElement();
            try {

                String msg = StringZipper.unzipStringFromBytes(bt);
//                System.out.println(msg);
                xml.parseString(msg);
                parse(xml, userMessage);
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    }


//    private ServerConnectionThread getTableController(
//            INonBlockingConnection nbc,
//            boolean forceCreate) {
//        String id = nbc.getId();
//        ServerConnectionThread tblController = tblControllers.get(id);
//        if (tblController == null && forceCreate) {
//            tblController = new ServerConnectionThread(nbc);
//        }
//        return tblController;
//    }
//
//    public void parse(XMLElement message, AUserMessage userMessage) throws
//            TransmissionException {
//        if (message != null) {
//
//            ServerConnectionThread tblController = getTableController(
//                    userMessage.getNbc(), true);
//            if (tblController != null) {
//                tblController.parse(message);
//            }
//        }
//    }
//
    private ServerTableHandler getTableController(
            INonBlockingConnection nbc,
            boolean forceCreate) {
        String id = nbc.getId();
        ServerTableHandler tblController = tblControllers.get(id);
        if (tblController == null && forceCreate) {
            tblController = new ServerTableHandler(nbc);
        }
        return tblController;
    }

    public void parse(XMLElement message, AUserMessage userMessage) throws
            TransmissionException {
        if (message != null) {

            ServerTableHandler tblController = getTableController(
                    userMessage.getNbc(), true);
            if (tblController != null) {
                try {
//                    tblController.notifyAll();
                } catch (Exception ex) {
//                    ex.printStackTrace();
                }
                tblController.parseTableMessage(message, "");
            }
        }
    }

}
